﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Areas.Admin.Models;
using ForzaGT3.Common.Configuration;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class PartnerController : Controller
    {
        //
        // GET: /Admin/Partner/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Name", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Partner> data = Partner.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Partner";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(PartnerModel model)
        {
            if (ModelState.IsValid)
            {
                Partner data = new Partner();
                data.PartnerId = Guid.NewGuid();
                data.Name = model.Name;
                data.Remarks = model.Remarks;
                if (model.LogoImage != null)
                {
                    data.LogoUrl = SiteSettings.PartnerImageFolder + model.LogoImage.FileName;
                    model.LogoImage.SaveAs(Server.MapPath("~" + SiteSettings.PartnerImageFolder) + model.LogoImage.FileName);   
                }
                data.CreatedDate = DateTime.Now;
                data.CreatedBy = HttpContext.User.Identity.Name;
                data.IsDeleted = false;
                data.Insert();

                return RedirectToAction(MVC.Admin.Partner.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            Partner data = Partner.GetById(Id);
            PartnerModel model = new PartnerModel();
            if (data != null)
            {
                model.Id = data.PartnerId;
                model.Name = data.Name;
                model.Remarks = data.Remarks;
                model.LogoUrl = data.LogoUrl;
            }
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(PartnerModel model)
        {
            Partner data = Partner.GetById(model.Id);
            if (data != null)
            {
                data.Name = model.Name;
                data.Remarks = model.Remarks;
                if (model.LogoImage != null)
                {
                    data.LogoUrl = SiteSettings.PartnerImageFolder + model.LogoImage.FileName;
                    model.LogoImage.SaveAs(Server.MapPath("~" + SiteSettings.PartnerImageFolder) + model.LogoImage.FileName);
                }

                data.ModifiedDate = DateTime.Now;
                data.ModifiedBy = HttpContext.User.Identity.Name;
                data.Update();

                return RedirectToAction(MVC.Admin.Partner.Index());
            }

            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Partner data = Partner.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }

            return RedirectToAction(MVC.Admin.Partner.Index());
        }
    }
}
